//
//  IWantPurchaseViewController.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/17.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "IWantPurchaseViewController.h"
#import "purchaseButton.h"
#import "textFieldView.h"
#import "getCommonDataMode.h"
#import "UIView+LQXkeyboard.h"
#import "OverlayTechnologyModel.h"
#import "XlineView.h"
#import "brandView.h"
@interface IWantPurchaseViewController ()<UIScrollViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate,UITextViewDelegate,brandViewDelegate,UIAlertViewDelegate>{
    int textFieldTag;
    int buttonTag;
    int height;
    int pinzhong;
    textFieldView *saveTextField;
}
@property (nonatomic,strong) UIScrollView *Scroll;
@property (nonatomic,strong) UIPickerView *picker;
@property (nonatomic,strong) NSMutableArray *pickerArray;
@property (nonatomic,strong) NSMutableArray *firstArray;
@property (nonatomic,strong) NSMutableArray *secondArray;
@property (nonatomic,strong) NSMutableArray *thirdArray;
@property (nonatomic,strong) NSMutableArray *provinceArray;
@property (nonatomic,strong) NSMutableArray *cityArray;
@property (nonatomic,strong) NSMutableDictionary *provinceDic;
@property (nonatomic,strong) NSMutableDictionary *cityDic;
@property (nonatomic,strong) NSMutableDictionary *scrollDic;
@property (nonatomic,strong) NSMutableArray *scrollViews;
@property (nonatomic,strong) plankModel *plankModel;
@property (nonatomic,strong) NSMutableArray *tjBrandData;
@property (nonatomic,strong) NSMutableDictionary *brandData;
@property (nonatomic,strong) NSMutableDictionary *pinzhongDic;
@property (nonatomic,strong) NSMutableDictionary *pinzhongSelectDic;
@end

@implementation IWantPurchaseViewController
@synthesize Scroll,picker,pickerArray,firstArray,secondArray,thirdArray,showType,plankDic,Type;
@synthesize plankString,selectType,provinceArray,cityArray,scrollViews,scrollDic;
@synthesize pinzhongDic,pinzhongSelectDic;
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)keyboardWillShow:(NSNotification*)notification
{
    //键盘弹出后的frame
    NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardBounds;
    [keyboardBoundsValue getValue:&keyboardBounds];
    
    //设置新的内边距，这个内边距是UIScrollView的最后一行距离UIScrollView底边框的距离，
    
    //这样当该函数触发式，系统会将当前选中行距离窗口底边的距离设为该值，从而正好不被键盘遮盖住。
//    self.Scroll.frame = CGRectMake(0, 74, SWIDTH, VHEIGHT);
//    for (UIView *view in [self.Scroll subviews]) {
//        if ([view isKindOfClass:[textFieldView class]]) {
//            textFieldView *text = (textFieldView *)view;
//            if (textFieldTag == text.tag) {
//                if ([text.tempLab.text isEqualToString:@"价格"]) {
//                    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height+20, 0);
//                    [[self Scroll] setContentInset:e];
//                    //调整滑动条距离窗口底边的距离
//                    [[self Scroll] setScrollIndicatorInsets:e];
//                    return;
//                }
//            }
//        }
//    }

    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
    [[self Scroll] setContentInset:e];
    //调整滑动条距离窗口底边的距离
    [[self Scroll] setScrollIndicatorInsets:e];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
    
    //键盘缩回后，恢复正常设置
    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, 0, 0);
    [[self Scroll] setScrollIndicatorInsets:e];
    [[self Scroll] setContentInset:e];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftBtn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    textFieldTag = 0;
    buttonTag = 0;
    // 监听键盘弹起
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    self.tjBrandData = [NSMutableArray array];
    self.pinzhongDic = [NSMutableDictionary dictionary];
    self.pinzhongSelectDic = [NSMutableDictionary dictionary];
    self.brandData = [NSMutableDictionary dictionary];
    NSString *filePath = FILE(@"getcommondata.txt");
    NSString *result = GETFILE(filePath);
    getCommonDataMode *model = [[getCommonDataMode alloc]init];
    [model parseResponseData:result];
    self.tjBrandData = [model.data objectForKey:@"tj_brands"];
    self.brandData = [model.data objectForKey:@"brands"];
    self.scrollViews = [NSMutableArray array];
    self.provinceDic = [NSMutableDictionary dictionary];
    self.scrollDic = [NSMutableDictionary dictionary];
    self.cityDic = [NSMutableDictionary dictionary];
    self.provinceArray = [NSMutableArray array];
    self.cityArray = [NSMutableArray array];
    [self superMainVC];
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"json"];
//    [self performSelectorOnMainThread:@selector(updateCity:) withObject:path waitUntilDone:YES];
//    品种 品牌 等级 贴面工艺 产品型号 花色 表面处理 包装 高度 宽度 厚度 数量 省份 城市 价格 货源信息 备注
    // Do any additional setup after loading the view.
}
-(void)goBackAction{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)superMainVC{
    // 刷新数据，在UI线程中更新UI
    dispatch_async(dispatch_get_main_queue(), ^{
        self.titleLab.text = @"我要采购";
        self.plankModel = [[plankModel alloc]init];
        self.plankDic = [NSMutableDictionary dictionary];
        self.mainData = [NSMutableArray array];
        self.firstArray = [NSMutableArray array];
        self.secondArray = [NSMutableArray array];
        self.thirdArray = [NSMutableArray array];
        self.pickerArray = [NSMutableArray arrayWithObjects:@"品种",@"品牌",@"货源地", nil];
        
        
        [self.rightBtn setTitle:@"完成" forState:0];
        [self.rightBtn addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.frame = CGRectMake(0, 74, SWIDTH, VHEIGHT); // frame中的size指UIScrollView的可视范围
        scrollView.delegate = self;
        scrollView.backgroundColor = [UIColor clearColor];
        self.Scroll = scrollView;
        [self.view addSubview:scrollView];
        self.Scroll.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTap:)];
        [self.Scroll addGestureRecognizer:tap];

        [self getPulank:0];
        
        purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:10];
        if (button) {
            [self buttonClick:button];
        }

    });
}
-(void)viewTap:(UIGestureRecognizer *)tap{
    for (UIView *view in [self.Scroll subviews]) {
        if ([view isKindOfClass:[textFieldView class]]) {
            textFieldView *tempView = (textFieldView *)view;
            [tempView.textField resignFirstResponder];
        }
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView *textView = (UITextView *)view;
            [textView resignFirstResponder];
        }
    }
}
//-(void)updateCity:(NSString *)path{
//    NSData *jsonData = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
//    NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
//    for (int i = 0; i < [arr count]; i++) {
//        NSMutableDictionary *province = [arr objectAtIndex:i];
//        if ([[province objectForKey:@"level"] isEqualToString:@"1"]) {
//            [provinceArray addObject:province];
//        }else if ([[province objectForKey:@"level"] isEqualToString:@"2"]){
//            [cityArray addObject:province];
//        }
//    }
//}
-(void)getPlankData{
    [self.firstArray removeAllObjects];
    [self.secondArray removeAllObjects];
    [self.plankDic removeAllObjects];
    NSString *filePath = FILE(@"getcommondata.txt");
    NSString *result = GETFILE(filePath);
    getCommonDataMode *model = [[getCommonDataMode alloc]init];
    [model parseResponseData:result];
    NSMutableArray *arr = [NSMutableArray array];
    NSMutableArray *tempArr1 = [NSMutableArray array];
    tempArr1 = [NSMutableArray arrayWithObjects:@"板材",@"门板",@"装饰纸", nil];
    self.firstArray = tempArr1;
    [arr addObject:[[model.data objectForKey:@"materials"] objectForKey:@"plank"]];
    [arr addObject:[[model.data objectForKey:@"materials"] objectForKey:@"doorplank"]];
    [arr addObject:[[model.data objectForKey:@"materials"] objectForKey:@"paper"]];
    [self.plankDic setObject:[[model.data objectForKey:@"materials"] objectForKey:@"plank"] forKey:@"板材"];
    [self.plankDic setObject:[[model.data objectForKey:@"materials"] objectForKey:@"doorplank"] forKey:@"门板"];
    [self.plankDic setObject:[[model.data objectForKey:@"materials"] objectForKey:@"paper"] forKey:@"装饰纸"];
    self.secondArray = [[model.data objectForKey:@"materials"] objectForKey:@"plank"];
}
-(void)getPulank:(buttonShowType )buttonType{
    [self.scrollDic removeAllObjects];
    [self.scrollViews removeAllObjects];
    NSArray *arr = [NSArray array];
    
    int material_id = [self.plankModel.material_id intValue];
    NSArray *arr0 = [NSArray array];
    NSArray *arr1 = [NSArray array];
    NSArray *arr2 = [NSArray array];
    NSArray *tempA0 = [NSArray arrayWithObjects:@"品种",@"品牌",@"等级",@"贴面工艺",@"花色",@"表面处理",@"产品型号",@"托盘包装",@"长度",@"宽度",@"厚度",@"数量",@"省份",@"城市",@"价格",@"货源信息", nil];
    arr0 = tempA0;
    NSArray *tempA3 = [NSArray arrayWithObjects:@"品种",@"品牌",@"等级",@"贴面工艺",@"花色",@"表面处理",@"产品型号",@"托盘包装",@"厚度",@"数量",@"省份",@"城市",@"价格",@"货源信息", nil];
    NSArray *tempA1 = [NSArray arrayWithObjects:@"品种",@"品牌",@"花色",@"树种",@"产品型号",@"高度",@"宽度",@"厚度",@"数量",@"省份",@"城市",@"价格", nil];
    arr1 = tempA1;
    NSArray *tempA2 = [NSArray arrayWithObjects:@"品种",@"品牌",@"花色",@"克重",@"产品型号",@"包装",@"长度",@"宽度",@"数量",@"省份",@"城市",@"价格", nil];
    arr2 = tempA2;
    NSArray *tempA4 = [NSArray arrayWithObjects:@"品种",@"品牌",@"花色",@"克重",@"产品型号",@"包装",@"数量",@"省份",@"城市",@"价格", nil];
    if (material_id >=200000000 && material_id <200003000) {
        arr2 = tempA4;
        arr0 = tempA3;
    }
    if (material_id >=200003000 && material_id <200007000) {
        arr0 = tempA0;
        arr2 = tempA2;
    }
    if (buttonType == 0) {
        arr = arr0;
    }else if (buttonType == 1){
        arr = arr1;
    }else{
        arr = arr2;
    }
    if (self.plankModel.material_id == nil) {
        arr = tempA0;
    }
    
    for (UIView *view in [self.Scroll subviews]) {
        if ([view isKindOfClass:[purchaseButton class]]) {
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[UITextField class]]) {
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[XlineView class]]) {
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[UITextView class]]) {
            [view resignFirstResponder];
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[textFieldView class]]) {
            textFieldView *tempView = (textFieldView *)view;
            [tempView.textField resignFirstResponder];
            [tempView removeFromSuperview];
        }
    }

    height = 0;
    for (int i = 0; i < [arr count]; i++) {
        if ([[arr objectAtIndex:i] isEqualToString:@"品种"]||[[arr objectAtIndex:i] isEqualToString:@"品牌"]||[[arr objectAtIndex:i] isEqualToString:@"等级"]||[[arr objectAtIndex:i] isEqualToString:@"贴面工艺"]||[[arr objectAtIndex:i] isEqualToString:@"花色"]||[[arr objectAtIndex:i] isEqualToString:@"省份"]||[[arr objectAtIndex:i] isEqualToString:@"城市"]||[[arr objectAtIndex:i] isEqualToString:@"货源信息"]||[[arr objectAtIndex:i] isEqualToString:@"树种"]||[[arr objectAtIndex:i] isEqualToString:@"克重"]) {
            purchaseButton *button = [[purchaseButton alloc]initWithFrame:CGRectMake(0, i*44, SWIDTH, 44)];
            [button setTitle:@"" forState:0];
            button.titleLab.text = @"";
            button.titleLab.textColor = UIColorFromRGB(0xd0d0d1);
            button.titleLab.frame = CGRectMake(120, (44 - 20)/2, SWIDTH - 180, 20);
            [button setBackgroundColor:[UIColor whiteColor]];
            button.tempLab.text = [arr objectAtIndex:i];
            if (i == 0) {
                if (![self.plankModel.name isEqualToString:@""]) {
//                    [button setTitle:self.plankModel.name forState:0];
                    button.titleLab.text = self.plankModel.name;
                    button.titleLab.textColor = GRAYColor;
                    button.modelId = self.plankModel.material_id;
                }
            }
            /**
             *  选填数据 ：花色，表面处理，产品型号，托盘包装，省份，城市，价格，长度，高度，宽度，包装
             */
            if ([self.title isEqualToString:@"我要采购"]) {
                if ([[arr objectAtIndex:i] isEqualToString:@"花色"]||[[arr objectAtIndex:i] isEqualToString:@"省份"]||[[arr objectAtIndex:i] isEqualToString:@"城市"]) {
//                    [button setTitle:@"选填" forState:0];
                    button.titleLab.text = @"选填";
                    if (buttonType !=0) {
                        if ([[arr objectAtIndex:i]isEqualToString:@"花色"]) {
//                            [button setTitle:@"" forState:0];
                            button.titleLab.text = @"";
                        }
                    }
                }
            }
            [self.scrollDic setObject:@"" forKey:@"texture"];
            [self.scrollDic setObject:@"" forKey:@"province"];
            [self.scrollDic setObject:@"" forKey:@"city"];
            if ([[arr objectAtIndex:i] isEqualToString:@"货源信息"]) {
                OverlayTechnologyModel *model = [[OverlayTechnologyModel alloc]init];
                model.modelId = @"0";
                model.name = @"现货";
                [self.scrollDic setObject:model.modelId forKey:@"future"];
//                [button setTitle:model.name forState:0];
                button.titleLab.text = model.name;
                button.titleLab.textColor = GRAYColor;
            }
            
            if (material_id >=200000000 && material_id <200003000) {

            }
            if (material_id >=200003000 && material_id <200007000) {

            }
            
            [button setTitleColor:UIColorFromRGB(0xd2d2d2) forState:0];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            button.titleLab.font = [UIFont systemFontOfSize:14];
            button.tag = i+10;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.Scroll addSubview:button];
            height = button.frame.origin.y+button.frame.size.height;
        }else{
            textFieldView *textField = [[textFieldView alloc]initWithFrame:CGRectMake(0, i*44, SWIDTH, 44)];
            textField.backgroundColor = [UIColor whiteColor];
            textField.tempLab.frame = CGRectMake(10, (44 - 20)/2, 85, 20);
            UIFont *font = [UIFont systemFontOfSize:14];
            textField.textField.font = font;
            textField.tempLab.font = font;
            textField.tempLab.text = [arr objectAtIndex:i];
            textField.tag = i+10;
            textField.textField.frame = CGRectMake(120, (44 - 20)/2, SWIDTH - 170, 20);
            if ([[arr objectAtIndex:i] isEqualToString:@"长度"]||[[arr objectAtIndex:i] isEqualToString:@"宽度"]||[[arr objectAtIndex:i] isEqualToString:@"高度"]||[[arr objectAtIndex:i] isEqualToString:@"厚度"]) {
                textField.tempLab1.text = @"mm";
                textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                if ([[arr objectAtIndex:i] isEqualToString:@"厚度"]) {
                    textField.textField.keyboardType = UIKeyboardTypeDecimalPad;
                }
            }
            
            if ([[arr objectAtIndex:i] isEqualToString:@"托盘包装"]) {
                textField.tempLab1.text = @"张/托盘";
                textField.textField.keyboardType=UIKeyboardTypeNumberPad;
            }
            if (buttonType == 0) {
                if ([[arr objectAtIndex:i] isEqualToString:@"数量"]) {
                    textField.tempLab1.text = @"张";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"价格"]) {
                    textField.tempLab1.text = @"元/张";
                    textField.textField.keyboardType=UIKeyboardTypeDecimalPad;
                }
            }else if (buttonType == 1) {
                if ([[arr objectAtIndex:i] isEqualToString:@"数量"]) {
                    textField.tempLab1.text = @"片";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"价格"]) {
                    textField.tempLab1.text = @"元/片";
                    textField.textField.keyboardType=UIKeyboardTypeDecimalPad;
                }
            }else{
//                purchaseButton *pinzhongButton = (purchaseButton *)[self.Scroll viewWithTag:10];
//                if ([pinzhongButton.titleLab.text isEqualToString:@"素色纸"]) {
//
//                }
                if ([[arr objectAtIndex:i] isEqualToString:@"长度"]||[[arr objectAtIndex:i] isEqualToString:@"宽度"]) {
                    textField.tempLab1.text = @"尺";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"数量"]) {
                    textField.tempLab1.text = @"吨";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"价格"]) {
                    textField.tempLab1.text = @"元/吨";
                    textField.textField.keyboardType=UIKeyboardTypeDecimalPad;
                }
            }
            
            if ([self.plankModel.material_id integerValue] >=200000000 && [self.plankModel.material_id integerValue] <=200003000) {
                if ([[arr objectAtIndex:i] isEqualToString:@"托盘包装"]) {
                    textField.tempLab1.text = @"吨";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"包装"]) {
                    textField.tempLab1.text = @"吨";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"数量"]) {
                    textField.tempLab1.text = @"吨";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"价格"]) {
                    textField.tempLab1.text = @"元/吨";
                    textField.textField.keyboardType=UIKeyboardTypeDecimalPad;
                }
            }
            if ([self.plankModel.material_id integerValue] >=200003000 && [self.plankModel.material_id integerValue] <=200007000) {
                if ([[arr objectAtIndex:i] isEqualToString:@"托盘包装"]) {
                    textField.tempLab1.text = @"张";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"包装"]) {
                    textField.tempLab1.text = @"张";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"数量"]) {
                    textField.tempLab1.text = @"张";
                    textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                }
                if ([[arr objectAtIndex:i] isEqualToString:@"价格"]) {
                    textField.tempLab1.text = @"元/张";
                    textField.textField.keyboardType=UIKeyboardTypeDecimalPad;
                }
            }
            textField.textField.placeholder = @"选填";
            if ([[arr objectAtIndex:i]isEqualToString:@"长度"]||[[arr objectAtIndex:i]isEqualToString:@"宽度"]||[[arr objectAtIndex:i]isEqualToString:@"厚度"]||[[arr objectAtIndex:i]isEqualToString:@"数量"]) {
                textField.textField.placeholder = @"";
                textField.textField.keyboardType=UIKeyboardTypeNumberPad;
                if ([[arr objectAtIndex:i]isEqualToString:@"厚度"]) {
                    textField.textField.keyboardType = UIKeyboardTypeDecimalPad;
                }
            }
            if ([[arr objectAtIndex:i]isEqualToString:@"长度"]||[[arr objectAtIndex:i]isEqualToString:@"宽度"]) {
                textField.textField.placeholder = @"选填";
                textField.textField.keyboardType=UIKeyboardTypeNumberPad;
            }
            if (![self.title isEqualToString:@"我要采购"]) {
                textField.textField.placeholder = @"";
            }
            textField.textField.clearButtonMode = UITextFieldViewModeAlways;
            textField.textField.tag = i+10;
            textField.textField.delegate = self;
            textField.textField.textColor = UIColorFromRGB(0x666666);
            [self.Scroll addSubview:textField];
            height = textField.frame.origin.y+textField.frame.size.height;
        }
        
        XlineView *lineView = [[XlineView alloc]initWithFrame:CGRectMake(10, 44*i, SWIDTH - 20, 0.5)];
        lineView.backgroundColor = [UIColor grayColor];
        lineView.alpha = 0.2;
        [self.Scroll addSubview:lineView];
    }
    XlineView *bgView = [[XlineView alloc]initWithFrame:CGRectMake(0, 10+height, SWIDTH, 90)];
    [bgView setBackgroundColor:[UIColor whiteColor]];
    [self.Scroll addSubview:bgView];
    
    UITextView *textView= [[UITextView alloc]initWithFrame:CGRectMake(0, 10+height, SWIDTH, 90)];
    textView.delegate = self;
    [textView setBackgroundColor:[UIColor clearColor]];
    textView.text = @"备注(选填)";
    textView.textColor = UIColorFromRGB(0x666666);
    textView.tag = 101;
    textView.textAlignment = NSTextAlignmentLeft;
    textView.font = [UIFont systemFontOfSize:14.0f];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.Scroll addSubview:textView];
    [self.Scroll setContentSize:CGSizeMake(SWIDTH, height+120)];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    for (UIView *view in [self.Scroll subviews]) {
        if ([view isKindOfClass:[textFieldView class]]) {
            textFieldView *tempView = (textFieldView *)view;
            if ([tempView.tempLab.text isEqualToString:@"价格"]) {
                BOOL isHaveDian = YES;
                if (tempView.textField == textField) {
                    if ([textField.text rangeOfString:@"."].location==NSNotFound) {
                        isHaveDian=NO;
                    }
                    if ([string length]>0)
                    {
                        unichar single=[string characterAtIndex:0];//当前输入的字符
                        if ((single >='0' && single<='9') || single=='.')//数据格式正确
                        {
                            //首字母不能为0和小数点
                            if([textField.text length]==0){
                                if(single == '.'){
                                    //                    [self alertView:@"亲，第一个数字不能为小数点"];
                                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                                    return NO;
                                }
                                if (single == '0') {
                                    //                    [self alertView:@"亲，第一个数字不能为0"];
                                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                                    return NO;
                                }
                            }
                            if (single=='.')
                            {
                                if(!isHaveDian)//text中还没有小数点
                                {
                                    isHaveDian=YES;
                                    return YES;
                                }else
                                {
                                    //                    [self alertView:@"亲，您已经输入过小数点了"];
                                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                                    return NO;
                                }
                            }
                            else
                            {
                                if (isHaveDian)//存在小数点
                                {
                                    //判断小数点的位数
                                    NSRange ran=[textField.text rangeOfString:@"."];
                                    int tt=range.location-ran.location;
                                    if (tt <= 2){
                                        return YES;
                                    }else{
                                        //                        [self alertView:@"亲，您最多输入两位小数"];
                                        return NO;
                                    }
                                }
                                else
                                {
                                    return YES;
                                }
                            }
                        }else{//输入的数据格式不正确
                            //            [self alertView:@"亲，您输入的格式不正确"];
                            [textField.text stringByReplacingCharactersInRange:range withString:@""];
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                    
                }
            }
            if ([tempView.tempLab.text isEqualToString:@"厚度"]) {
                BOOL isHaveDian = YES;
                if (tempView.textField == textField) {
                    if ([textField.text rangeOfString:@"."].location==NSNotFound) {
                        isHaveDian=NO;
                    }
                    if ([string length]>0)
                    {
                        unichar single=[string characterAtIndex:0];//当前输入的字符
                        if ((single >='0' && single<='9') || single=='.')//数据格式正确
                        {
                            //首字母不能为0和小数点
                            if([textField.text length]==0){
                                if(single == '.'){
                                    //                    [self alertView:@"亲，第一个数字不能为小数点"];
                                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                                    return NO;
                                }
                                if (single == '0') {
                                    //                    [self alertView:@"亲，第一个数字不能为0"];
                                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                                    return NO;
                                }
                            }
                            if (single=='.')
                            {
                                if(!isHaveDian)//text中还没有小数点
                                {
                                    isHaveDian=YES;
                                    return YES;
                                }else
                                {
                                    //                    [self alertView:@"亲，您已经输入过小数点了"];
                                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                                    return NO;
                                }
                            }
                            else
                            {
                                if (isHaveDian)//存在小数点
                                {
                                    //判断小数点的位数
                                    NSRange ran=[textField.text rangeOfString:@"."];
                                    int tt=range.location-ran.location;
                                    if (tt <= 1){
                                        return YES;
                                    }else{
                                        //                        [self alertView:@"亲，您最多输入两位小数"];
                                        return NO;
                                    }
                                }
                                else
                                {
                                    return YES;
                                }
                            }
                        }else{//输入的数据格式不正确
                            //            [self alertView:@"亲，您输入的格式不正确"];
                            [textField.text stringByReplacingCharactersInRange:range withString:@""];
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                    
                }

            }
        }
    }
    return YES;
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:10];
    [self removePickerView];
//    self.Scroll.frame = CGRectMake(0, 74, SWIDTH, VHEIGHT - 260);

    if ([button.titleLab.text isEqualToString:@""]) {
        [textView resignFirstResponder];
        SHOW_ALERT(@"请先选择品种");
        return;
    }
    if ([textView.text isEqualToString:@"备注(选填)"]) {
        textView.text = @"";
        textView.textColor = BLACKColor;
    }
    CGFloat offset_y = 470.f;
    CGPoint point = self.Scroll.contentOffset;
    point = CGPointMake(point.x, offset_y);
    [UIView animateWithDuration:0.25 animations:^{
        self.Scroll.contentOffset = point;
    }];
}
-(void)textViewDidEndEditing:(UITextView *)textView{
//    self.Scroll.frame = CGRectMake(0, 74, SWIDTH, VHEIGHT);
    if ([textView.text isEqualToString:@""]) {
        textView.text = @"备注(选填)";
        textView.textColor = GRAYColor;
    }
    CGPoint point = self.Scroll.contentOffset;
    [UIView animateWithDuration:0.25 animations:^{
        self.Scroll.contentOffset = CGPointMake(point.x, 0);
    }];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textFieldTag = textField.tag;
//    CGPoint point = self.Scroll.contentOffset;
//    [UIView animateWithDuration:0.25 animations:^{
//        self.Scroll.contentOffset = CGPointMake(point.x, 0);
//    }];
    purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:10];
    if ([button.titleLab.text isEqualToString:@""]) {
        [textField resignFirstResponder];
        SHOW_ALERT(@"请先选择品种");
        return;
    }
    [self removePickerView];
    CGFloat offset_y = 200.f;
    CGPoint point = self.Scroll.contentOffset;
    point = CGPointMake(point.x, offset_y);
    [UIView animateWithDuration:0.25 animations:^{
        self.Scroll.contentOffset = point;
    }];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    CGPoint point = self.Scroll.contentOffset;
    [UIView animateWithDuration:0.25 animations:^{
        self.Scroll.contentOffset = CGPointMake(point.x, 0);
    }];
}

-(void)confirmPickView{
    //TODO:确定
    UIView *tempView = (UIView *)[self.view viewWithTag:220];
    UIPickerView *pickV = (UIPickerView *)[tempView viewWithTag:221];
    if (showType == 1) {
        int select = [pickV selectedRowInComponent:0];
        if ([[self.firstArray objectAtIndex:select]isEqualToString:@"板材"]) {
            self.Type = 0;
        }else if ([[self.firstArray objectAtIndex:select]isEqualToString:@"门板"]) {
            self.Type = 1;
        }else if ([[self.firstArray objectAtIndex:select]isEqualToString:@"装饰纸"]) {
            self.Type = 2;
        }
        self.plankModel = [self.secondArray objectAtIndex:[pickV selectedRowInComponent:1]];
        [self getPulank:self.Type];
        [self.scrollDic setObject:self.plankModel.material_id forKey:@"material_id"];
        [self.firstArray removeAllObjects];
        [self.secondArray removeAllObjects];
        [self.plankDic removeAllObjects];
    }else{
        purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:buttonTag];
        if ([button.tempLab.text isEqualToString:@"品牌"]) {
            tj_brandsModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
        }else if ([button.tempLab.text isEqualToString:@"等级"]){
            eplevelsModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
            [self.scrollDic setObject:model.name forKey:@"eplevel"];
        }else if ([button.tempLab.text isEqualToString:@"贴面工艺"]){
            OverlayTechnologyModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
            [self.scrollDic setObject:model.modelId forKey:@"veneer"];
        }else if ([button.tempLab.text isEqualToString:@"花色"]){
            texturesModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
            [self.scrollDic setObject:model.name forKey:@"texture"];
        }else if ([button.tempLab.text isEqualToString:@"省份"]){
            NSDictionary *dic = [[appStatus shareAppStatus].provinceArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = [dic objectForKey:@"city"];
            [self.scrollDic setObject:[dic objectForKey:@"id"] forKey:@"province"];
            purchaseButton *buttonCity = (purchaseButton *)[self.Scroll viewWithTag:buttonTag+1];
            if ([self.title isEqualToString:@"我要采购"]) {
                buttonCity.titleLab.text = @"选填";
            }else{
                buttonCity.titleLab.text = @"";
            }
            [self.scrollDic setObject:@"" forKey:@"city"];
        }else if ([button.tempLab.text isEqualToString:@"城市"]){
            NSDictionary *dic = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = [dic objectForKey:@"city"];
            [self.scrollDic setObject:[dic objectForKey:@"id"] forKey:@"city"];
        }else if ([button.tempLab.text isEqualToString:@"树种"]){
            stuffsModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
            [self.scrollDic setObject:model.name forKey:@"stuff"];//stuff
        }else if ([button.tempLab.text isEqualToString:@"克重"]){
            gramsModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
            [self.scrollDic setObject:model.name forKey:@"gram"];
        }else if ([button.tempLab.text isEqualToString:@"货源信息"]){
            OverlayTechnologyModel *model = [self.thirdArray objectAtIndex:[pickV selectedRowInComponent:0]];
            button.titleLab.text = model.name;
            [self.scrollDic setObject:model.modelId forKey:@"future"];
        }
        button.titleLab.textColor = UIColorFromRGB(0x666666);
    }
    if (pinzhong == 1) {
        pinzhong = 0;
        for (int i = 0; i <[[self.pinzhongSelectDic allKeys] count]; i++) {
            NSString *key = [[self.pinzhongSelectDic allKeys] objectAtIndex:i];
            NSString *value = [self.pinzhongSelectDic objectForKey:key];
            [self.pinzhongDic setObject:value forKey:key];
        }
        [self.pinzhongSelectDic removeAllObjects];
    }
    if (tempView) {
        [tempView removeFromSuperview];
        tempView = nil;
    }
}
-(void)pickerHide{
    [self removePickerView];
}

-(void)removePickerView{
    buttonTag = 0;
    [self.firstArray removeAllObjects];
    [self.secondArray removeAllObjects];
    [self.plankDic removeAllObjects];
    UIView *tempView = (UIView *)[self.view viewWithTag:220];
    if (tempView) {
        [tempView removeFromSuperview];
        tempView = nil;
    }
    if (pinzhong == 1) {
        pinzhong = 0;
        purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:10];
//        if ([[button currentTitle]isEqualToString:@""]) {
//            [self.pinzhongSelectDic removeAllObjects];
//        }
        if ([button.titleLab.text isEqualToString:@""]) {
            [self.pinzhongSelectDic removeAllObjects];
        }
    }
}
-(void)returnPickerView{
    UIView *tempView1 = (UIView *)[self.view viewWithTag:220];
    UIPickerView *pickV = (UIPickerView *)[tempView1 viewWithTag:221];
    if (pickV) {
        [pickV removeFromSuperview];
        pickV = nil;
    }
    UIToolbar *toolbar = (UIToolbar *)[tempView1 viewWithTag:223];
    if (toolbar) {
        [toolbar removeFromSuperview];
        toolbar = nil;
    }
    if (tempView1) {
        [tempView1 removeFromSuperview];
        tempView1 = nil;
    }
    UIView *tempView = (UIView *)[self createViewFrame:CGRectMake(0, SHEIGHT - 260, SWIDTH, 260) bgColor:[UIColor whiteColor] tag:220];
    [self.view addSubview:tempView];

    //创建工具栏
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
    UIBarButtonItem *confirmBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(confirmPickView)];
    UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(pickerHide)];
    [items addObject:cancelBtn];
    [items addObject:flexibleSpaceItem];
    [items addObject:confirmBtn];
    
    UIPickerView *tempPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, SWIDTH, 216)];
    tempPicker.tag = 221;
    tempPicker.delegate = self;
    tempPicker.dataSource = self;
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 44)];
    toolBar.tag = 223;
    toolBar.hidden = NO;
    toolBar.barStyle = UIBarStyleDefault;
    toolBar.items = items;
    [tempView addSubview:toolBar];
    [tempView addSubview:tempPicker];

}
-(void)removeBrandView{
    UIView *bgView =(UIView *)[self.navigationController.view viewWithTag:890];
    brandView *supplyV = (brandView *)[self.view viewWithTag:111];
    if (supplyV) {
        [supplyV removeFromSuperview];
        supplyV = nil;
    }
    if (bgView) {
        [bgView removeFromSuperview];
        bgView = nil;
    }
}
/**
 *  按钮 品种，品牌，等级，贴面工艺，花色，省份，城市，货源信息，树种，克重
 */
-(void)buttonClick:(purchaseButton *)sender{
    CGPoint point = self.Scroll.contentOffset;
    [UIView animateWithDuration:0.25 animations:^{
        self.Scroll.contentOffset = CGPointMake(point.x, 0);
    }];
     buttonTag = 0;
    [self removePickerView];
    UIView *bgView =(UIView *)[self.navigationController.view viewWithTag:890];
    brandView *supplyV = (brandView *)[self.view viewWithTag:111];
    if (supplyV) {
        [supplyV removeFromSuperview];
        supplyV = nil;
    }
    if (bgView) {
        [bgView removeFromSuperview];
        bgView = nil;
    }
    UITextView *tempTextView = (UITextView *)[self.Scroll viewWithTag:101];
    [tempTextView resignFirstResponder];
    if (textFieldTag!=0) {
        textFieldView *textField = (textFieldView *)[self.Scroll viewWithTag:textFieldTag];
        [textField.textField resignFirstResponder];
        textFieldTag = 0;
    }
    purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:10];
    buttonTag = sender.tag;
//    if ([button.titleLabel.text isEqualToString:@""] || [button currentTitle] == nil) {
//        if (sender.tag != 10) {
//            SHOW_ALERT(@"请先选择品种");
//            return;
//        }
//    }
    if ([button.titleLab.text isEqualToString:@""]) {
        if (sender.tag != 10) {
            SHOW_ALERT(@"请先选择品种");
            return;
        }
    }
    if ([sender.tempLab.text isEqualToString:@"品种"]) {
        self.showType = 1;
        [self getPlankData];
        pinzhong = 1;
        
    }else if ([sender.tempLab.text isEqualToString:@"品牌"]){
        self.showType = 0;
        self.selectType = 0;
        [self getOtherArray:sender];
        // 品牌
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeBrandView)];
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 64)];
        bgView.tag = 890;
        [bgView setBackgroundColor:[UIColor clearColor]];
        [bgView addGestureRecognizer:tap];
        bgView.userInteractionEnabled = YES;
        [self.navigationController.view addSubview:bgView];
        brandView *brandV = [[brandView alloc]initWithFrame:CGRectMake(0, 64, SWIDTH, VHEIGHT)];
        brandV.mainData = self.brandData;
        brandV.tjData = self.tjBrandData;
        brandV.delegate = self;
//        UILabel *lab = (UILabel *)[sender viewWithTag:2];
        brandV.selectString = sender.titleLab.text;
        brandV.tag = 111;
        [brandV setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:brandV];
        [brandV reloadView];
    }else if ([sender.tempLab.text isEqualToString:@"等级"]){
        self.showType = 0;
        self.selectType =eplevelsType;
        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"贴面工艺"]){
        self.showType = 0;
        self.selectType = overlayTechnology;
        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"花色"]){
        self.showType = 0;
        self.selectType = texturesType;
        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"省份"]){
        self.showType = 0;
        self.selectType = provinceType;
//        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"城市"]){
        purchaseButton *tempButton = (purchaseButton *)[self.Scroll viewWithTag:sender.tag - 1];
//        if ([button.titleLabel.text isEqualToString:@""] || [tempButton currentTitle] == nil) {
//            SHOW_ALERT(@"请先选择省份");
//            return;
//        }
        if ([tempButton.titleLab.text isEqualToString:@""]||[tempButton.titleLab.text isEqualToString:@"选填"]) {
            SHOW_ALERT(@"请先选择省份");
            return;
        }
        self.showType = 0;
        self.selectType = cityType;
        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"货源信息"]){
        self.showType = 0;
        self.selectType = goodsInformation;
        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"树种"]){
        self.showType = 0;
        self.selectType = stuffsType;
        [self getOtherArray:sender];
    }else if ([sender.tempLab.text isEqualToString:@"克重"]){
        self.showType = 0;
        self.selectType = gramsType;
        [self getOtherArray:sender];
    }
    if (![sender.tempLab.text isEqualToString:@"品牌"]){
        [self returnPickerView];
        if (pinzhong == 1) {
            if (![sender.titleLab.text isEqualToString:@""]) {
                UIView *tempView1 = (UIView *)[self.view viewWithTag:220];
                UIPickerView *pickV = (UIPickerView *)[tempView1 viewWithTag:221];
                if ([pinzhongDic count]!=0) {
                    int com0 = [[pinzhongDic objectForKey:@"0"] intValue];
                    NSString *key = [self.firstArray objectAtIndex:com0];
                    self.secondArray=[self.plankDic objectForKey:key];
                    [pickV selectRow:com0 inComponent:0 animated:YES];
                    [pickV reloadComponent:1];
                    int com1 = [[pinzhongDic objectForKey:@"1"] intValue];
                    [pickV selectRow:com1 inComponent:1 animated:YES];
                }
            }
        }else{
            UIView *tempView1 = (UIView *)[self.view viewWithTag:220];
            UIPickerView *pickV = (UIPickerView *)[tempView1 viewWithTag:221];
            purchaseButton *button = (purchaseButton *)[self.Scroll viewWithTag:buttonTag];
            if (self.selectType == 0) {
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        tj_brandsModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == eplevelsType){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        eplevelsModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == texturesType){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        eplevelsModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == stuffsType){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        stuffsModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == gramsType){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        gramsModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == overlayTechnology){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        OverlayTechnologyModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == goodsInformation){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        OverlayTechnologyModel *model = [self.thirdArray objectAtIndex:i];
                        if ([model.name isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == provinceType){
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [[appStatus shareAppStatus].provinceArray count]; i++) {
                        NSDictionary *dic = [[appStatus shareAppStatus].provinceArray objectAtIndex:i];
                        if ([[dic objectForKey:@"city"] isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }else if (self.selectType == cityType){
                purchaseButton *tempButton = (purchaseButton *)[self.Scroll viewWithTag:buttonTag - 1];
                NSString *cityId = [NSString string];
                NSMutableArray *arr = [NSMutableArray array];
                for (int i = 0; i <[[appStatus shareAppStatus].provinceArray count]; i++) {
                    NSMutableDictionary *dic = [[appStatus shareAppStatus].provinceArray objectAtIndex:i];
                    if ([[dic objectForKey:@"city"] isEqualToString:tempButton.titleLab.text]) {
                        cityId = [dic objectForKey:@"id"];
                    }
                }
                for (int k = 0; k < [[appStatus shareAppStatus].cityArray count]; k++) {
                    NSMutableDictionary *dic = [[appStatus shareAppStatus].cityArray objectAtIndex:k];
                    if ([[dic objectForKey:@"province_id"] isEqualToString:cityId]) {
                        [arr addObject:dic];
                    }
                }
                self.thirdArray = arr;
                if (![button.titleLab.text isEqualToString:@""]&&![button.titleLab.text isEqualToString:@"选填"]) {
                    for (int i = 0; i < [self.thirdArray count]; i++) {
                        NSDictionary *dic = [self.thirdArray objectAtIndex:i];
                        if ([[dic objectForKey:@"city"] isEqualToString:button.titleLab.text]) {
                            [pickV selectRow:i inComponent:0 animated:YES];
                        }
                    }
                }
            }
        }
    }
    
}
/**
 *  按钮 品种，品牌，等级，贴面工艺，花色，省份，城市，货源信息，树种，克重
 */

-(void)getOtherArray:(purchaseButton *)sender{
    [self.thirdArray removeAllObjects];
    NSString *filePath = FILE(@"getcommondata.txt");
    NSString *result = GETFILE(filePath);
    getCommonDataMode *model = [[getCommonDataMode alloc]init];
    [model parseResponseData:result];
    NSMutableArray *arr = [NSMutableArray array];
    if ([sender.tempLab.text isEqualToString:@"品牌"]) {
       NSMutableDictionary *dic = [model.data objectForKey:@"brands"];
        for (int i = 0; i <[dic count]; i++) {
            NSArray *array = [dic objectForKey:[[dic allKeys] objectAtIndex:i]];
            [arr addObjectsFromArray:array];
        }
    }else if ([sender.tempLab.text isEqualToString:@"等级"]){
        arr = [model.data objectForKey:@"eplevels"];
    }else if ([sender.tempLab.text isEqualToString:@"贴面工艺"]){
        NSArray *array = [NSArray arrayWithObjects:@"无",@"双饰面板",@"素板", nil];
        for (int i = 0; i <[array count]; i++) {
            OverlayTechnologyModel *model = [[OverlayTechnologyModel alloc]init];
            model.modelId = [NSString stringWithFormat:@"%d",i];
            model.name = [array objectAtIndex:i];
            [arr addObject:model];
        }
    }else if ([sender.tempLab.text isEqualToString:@"花色"]){
        arr = [model.data objectForKey:@"textures"];
    }else if ([sender.tempLab.text isEqualToString:@"省份"]){
        arr = [appStatus shareAppStatus].provinceArray;
    }else if ([sender.tempLab.text isEqualToString:@"城市"]){
        arr = [appStatus shareAppStatus].cityArray;
    }else if ([sender.tempLab.text isEqualToString:@"树种"]){
        arr = [model.data objectForKey:@"stuffs"];
    }else if ([sender.tempLab.text isEqualToString:@"克重"]){
        arr = [model.data objectForKey:@"grams"];
    }else if ([sender.tempLab.text isEqualToString:@"货源信息"]){
        NSArray *array = [NSArray arrayWithObjects:@"现货",@"期货", nil];
        for (int i = 0; i <[array count]; i++) {
            OverlayTechnologyModel *model = [[OverlayTechnologyModel alloc]init];
            model.modelId = [NSString stringWithFormat:@"%d",i];
            model.name = [array objectAtIndex:i];
            [arr addObject:model];
        }
    }
    self.thirdArray = arr;
}
/**
 *  选填数据 ：花色，表面处理，产品型号，托盘包装，省份，城市，价格，长度，高度，宽度，包装
 */
/**
 *  按钮
 *
 *  @param sender 品种，品牌，等级，贴面工艺，花色，省份，城市，货源信息，树种，克重，
 */
/**
 *  输入框
 *
 *  @param sender 表面处理，产品型号，托盘包装，包装，厚度，数量，价格，宽度，高度
 */
-(void)sure:(UIButton *)sender{
    [self removePickerView];
    for (UIView *view in [self.Scroll subviews]) {
        if ([view isKindOfClass:[purchaseButton class]]) {
            purchaseButton *button = (purchaseButton *)view;
//            if ([button.titleLabel.text isEqualToString:@""] || button.titleLabel.text == nil) {
//                NSString *msg = [NSString stringWithFormat:@"请选择%@",button.tempLab.text];
//                SHOW_ALERT(msg);
//                return;
//            }
            if ([button.titleLab.text isEqualToString:@""]) {
                NSString *msg = [NSString stringWithFormat:@"请选择%@",button.tempLab.text];
                SHOW_ALERT(msg);
                return;
            }
            if ([button.tempLab.text isEqualToString:@"花色"] || [button.tempLab.text isEqualToString:@"省份"]|| [button.tempLab.text isEqualToString:@"城市"]) {
//                if ([button.titleLabel.text isEqualToString:@"选填"]) {
//                    NSLog(@"不添加");
//                }
                if ([button.titleLab.text isEqualToString:@"选填"]) {
                    NSLog(@"不添加");
                }
            }
            
        }
        if ([view isKindOfClass:[textFieldView class]]) {
            textFieldView *tempView = (textFieldView *)view;
            if (![tempView.textField.placeholder isEqualToString:@"选填"]) {
                if ([tempView.textField.text isEqualToString:@""]) {
                    NSString *msg = [NSString stringWithFormat:@"请填写%@",tempView.tempLab.text];
                    SHOW_ALERT(msg);
                    return;
                }
            }
        }
        if ([view isKindOfClass:[textFieldView class]]) {
            textFieldView *tempView = (textFieldView *)view;
            [tempView.textField resignFirstResponder];
            if (![tempView.textField.text isEqualToString:@""]) {
                if ([tempView.tempLab.text isEqualToString:@"价格"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"price"]; //价格
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"price"];
                    }
                }
                
                if ([tempView.tempLab.text isEqualToString:@"托盘包装"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"unit_number"]; //托盘包装
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"unit_number"];
                    }
                }
                
                if ([tempView.tempLab.text isEqualToString:@"包装"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"unit_number"]; //包装
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"unit_number"];
                    }
                }
                
                if ([tempView.tempLab.text isEqualToString:@"产品型号"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"model"]; //产品型号
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"model"];
                    }
                }
                if ([tempView.tempLab.text isEqualToString:@"数量"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"number"]; //数量
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"number"];
                    }
                }
                if ([tempView.tempLab.text isEqualToString:@"高度"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"length"]; //高度
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"length"];
                    }
                }
                if ([tempView.tempLab.text isEqualToString:@"宽度"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"width"]; //宽度
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"width"];
                    }
                }
                if ([tempView.tempLab.text isEqualToString:@"厚度"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"thickness"]; //厚度
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"thickness"];
                    }
                }
                if ([tempView.tempLab.text isEqualToString:@"表面处理"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"surface"]; //表面处理
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"surface"];
                    }
                }
                if ([tempView.tempLab.text isEqualToString:@"长度"]) {
                    if ([tempView.textField.text isEqualToString:@""] || tempView.textField.text == nil){
                        [self.scrollDic setObject:@"" forKey:@"length"]; //长度
                    }else{
                        [self.scrollDic setObject:tempView.textField.text forKey:@"length"];
                    }
                }
            }
        }
    }
    NSString *url = [NSString string];
    if ([self.title isEqualToString:@"我要采购"]) {
        if (self.Type == 0) {
            url = @"/profile/addpurchase";
        }else if (self.Type == 1){
            url = @"/profile/addpurchase300";
        }else if (self.Type == 2){
            url = @"/profile/addpurchase200";
        }
    }else{
        if (self.Type == 0) {
            url = @"/profile/addsupply";
        }else if (self.Type == 1){
            url = @"/profile/addsupply300";
        }else if (self.Type == 2){
            url = @"/profile/addsupply200";
        }
    }
    TIME
    HEADDIC(nowTime, url);
    UITextView *textView = (UITextView *)[self.Scroll viewWithTag:101];
    [self.scrollDic setObject:@"" forKey:@"id"];
    [self.scrollDic setObject:@"" forKey:@"remark"];

    if (![textView.text isEqualToString:@""]||![textView.text isEqualToString:@"备注(选填)"]) {
        [self.scrollDic setObject:textView.text forKey:@"remark"];
    }
    requestManage *request = [[requestManage alloc]init];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:HEAD forKey:@"head"];
    [dic setObject:self.scrollDic forKey:@"body"];
    NSString *result = [request post:APPIP postDataDic:dic error:nil];
    NSMutableDictionary *json = [result objectFromJSONString];
    NSString *errCode = [[json objectForKey:@"err_code"] stringValue];
    NSString *errMessage = [json objectForKey:@"err_message"];
    if (![errCode isEqualToString:@"0"]) {
        SHOW_ALERT(errMessage);
    }else{
        UIAlertView *sureAlert = [[UIAlertView alloc]initWithTitle:@"发布成功" message:errMessage delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        sureAlert.tag = 401;
        [sureAlert show];
    }
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 401) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)selectBrand:(tj_brandsModel*)dic{
    purchaseButton *button = (purchaseButton *)[self.view viewWithTag:11];
//    [button setTitle:dic.name forState:0];
    button.titleLab.text = dic.name;
    button.titleLab.textColor = UIColorFromRGB(0x666666);
    [self.scrollDic setObject:dic.brand_id forKey:@"brand_id"];
    brandView *supplyV = (brandView *)[self.view viewWithTag:111];
    if (supplyV) {
        [supplyV removeFromSuperview];
        supplyV = nil;
    }
}

#pragma mark --
#pragma mark--UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.showType == 1) {
        return 2;
    }else if(self.showType == 0){
        return 1;
    }
    return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.showType == showPickerTwo) {
        if (component==0) {
            return [self.firstArray count];
        }
        if (component==1) {
            return [self.secondArray count];
        }
    }else{
        if (self.selectType == provinceType){
            return [[appStatus shareAppStatus].provinceArray count];
        }else if (self.selectType == cityType){
            purchaseButton *tempButton = (purchaseButton *)[self.Scroll viewWithTag:buttonTag - 1];
            NSString *cityId = [NSString string];
            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 0; i <[[appStatus shareAppStatus].provinceArray count]; i++) {
                NSMutableDictionary *dic = [[appStatus shareAppStatus].provinceArray objectAtIndex:i];
//                if ([[dic objectForKey:@"city"] isEqualToString:tempButton.titleLabel.text]) {
//                    cityId = [dic objectForKey:@"id"];
//                }
                if ([[dic objectForKey:@"city"] isEqualToString:tempButton.titleLab.text]) {
                    cityId = [dic objectForKey:@"id"];
                }
            }
            for (int k = 0; k < [[appStatus shareAppStatus].cityArray count]; k++) {
                NSMutableDictionary *dic = [[appStatus shareAppStatus].cityArray objectAtIndex:k];
                if ([[dic objectForKey:@"province_id"] isEqualToString:cityId]) {
                    [arr addObject:dic];
                }
            }
            self.thirdArray = arr;
            return [self.thirdArray count];
        }
        return [self.thirdArray count];
    }
    return 0;
}


#pragma mark--
#pragma mark--UIPickerViewDelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (self.showType ==1) {
        if (component==0) {
            return [self.firstArray objectAtIndex:row];
        }
        if (component==1) {
            plankModel *model = [self.secondArray objectAtIndex:row];
            return model.name;
        }
    }else{
        if (self.selectType == 0) {
            tj_brandsModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == eplevelsType){
            eplevelsModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == texturesType){
            eplevelsModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == stuffsType){
            stuffsModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == gramsType){
            gramsModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == overlayTechnology){
            OverlayTechnologyModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == goodsInformation){
            OverlayTechnologyModel *model = [self.thirdArray objectAtIndex:row];
            return model.name;
        }else if (self.selectType == provinceType){
            NSDictionary *dic = [[appStatus shareAppStatus].provinceArray objectAtIndex:row];
            return [dic objectForKey:@"city"];
        }else if (self.selectType == cityType){
            purchaseButton *tempButton = (purchaseButton *)[self.Scroll viewWithTag:buttonTag - 1];
            NSString *cityId = [NSString string];
            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 0; i <[[appStatus shareAppStatus].provinceArray count]; i++) {
                NSMutableDictionary *dic = [[appStatus shareAppStatus].provinceArray objectAtIndex:i];
//                if ([[dic objectForKey:@"city"] isEqualToString:tempButton.titleLabel.text]) {
//                    cityId = [dic objectForKey:@"id"];
//                }
                if ([[dic objectForKey:@"city"] isEqualToString:tempButton.titleLab.text]) {
                    cityId = [dic objectForKey:@"id"];
                }
            }
            for (int k = 0; k < [[appStatus shareAppStatus].cityArray count]; k++) {
                NSMutableDictionary *dic = [[appStatus shareAppStatus].cityArray objectAtIndex:k];
                if ([[dic objectForKey:@"province_id"] isEqualToString:cityId]) {
                    [arr addObject:dic];
                }
            }
            self.thirdArray = arr;
            NSDictionary *dic = [self.thirdArray objectAtIndex:row];
            return [dic objectForKey:@"city"];
        }
        
    }
    return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"row is %d,Component is %d",row,component);
    if (showType == 1) {
        if (pinzhong == 1) {
            if (component == 0) {
                [pinzhongSelectDic removeAllObjects];
                [pinzhongSelectDic setObject:[NSNumber numberWithInteger:row] forKey:@"0"];
                [pinzhongSelectDic setObject:[NSNumber numberWithInteger:0] forKey:@"1"];
                [pickerView selectRow:0 inComponent:1 animated:YES];
            }else{
                [pinzhongSelectDic setObject:[NSNumber numberWithInteger:row] forKey:@"1"];
            }
        }
        if (component==0) {
            NSString *key = [self.firstArray objectAtIndex:row];
            self.secondArray=[self.plankDic objectForKey:key];
            [pickerView selectedRowInComponent:0];
            [pickerView reloadComponent:0];
            [pickerView selectedRowInComponent:1];
            [pickerView reloadComponent:1];
        }
    }else{
        if (self.selectType == 0) {
            tj_brandsModel *brandModel = [self.thirdArray objectAtIndex:row];
            NSLog(@"name___%@",brandModel.name);
        }else if (self.selectType == eplevelsType){
            
        }else if (self.selectType == texturesType){
            
        }else if (self.selectType == stuffsType){
            
        }else if (self.selectType == gramsType){
            
        }else if (self.selectType == overlayTechnology){
            
        }else if (self.selectType == goodsInformation){
            
        }else if (self.selectType == provinceType){
            self.provinceDic = [[appStatus shareAppStatus].provinceArray objectAtIndex:row];
        }else if (self.selectType == cityType){
            self.cityDic = [self.thirdArray objectAtIndex:row];
        }
    }
}


//-(void)textFieldDidEndEditing:(UITextField *)textField{
//    NSInteger row = [selectPicker selectedRowInComponent:0];
//    self.textField.text = [pickerArray objectAtIndex:row];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
